# -*- coding: utf-8 -*-
# @Time:    2023-05-25 20:45
# @Author:  geng
# @Email:   yonglonggeng@163.com
# @WeChat:  ddgetget
# @File:    BatchRenameLabel4Abnormal.py
# @Project: opencv_toturial
# @Package: 
# @Ref:
# load：针对文件句柄，将json格式的字符转换为dict，从文件中读取 (将string转换为dict)
import json
import os
import shutil


def rename_path():
    for json_file in [json_file for json_file in os.listdir("./") if json_file.split(".")[-1] == "json"]:
        a_json = json.load(open('{}'.format(json_file), 'r', encoding='utf-8'))
        # print(a_json,type(a_json))

        a_json['imagePath'] = 'two_{}.jpg'.format(json_file.split(".")[0])
        # print(shape['label'])

        json.dump(a_json, open('two_{}'.format(json_file), 'w', encoding='utf-8'), indent=2, ensure_ascii=True)
        os.remove(json_file)

    for jpg_file in [jpg_file for jpg_file in os.listdir("./") if jpg_file.split(".")[-1] == "jpg"]:
        os.rename(jpg_file, "two_" + jpg_file)


def rename_label():
    try:
        shutil.rmtree("new_data")
    except:
        print("之前生成新数据")
    finally:
        os.mkdir("new_data")
        print("新文数据生成啦！！！")
    for file in os.listdir("data"):

        a_json = json.load(open('data/{}'.format(file), 'r', encoding='utf-8'))
        # print(a_json,type(a_json))

        for shape in a_json["shapes"]:
            shape['label'] = 'abnormal'
            # print(shape['label'])

        json.dump(a_json, open('new_data/{}'.format(file), 'w', encoding='utf-8'), indent=2, ensure_ascii=True)


if __name__ == '__main__':
    # rename_label()
    rename_path()
